import store from "@/store";
import conf from '../../router/meun'
export default {
    state: {
        /* page: [], */
        currentTabIndex: conf.homeTab.menuId, // 当前显示的 tab
        homeTabMenuId: conf.homeTab.menuId, // 主页 tab 的 menuId
        openedTabs: [ // 当前打开的 tab 列表
            conf.homeTab
        ],
        newItem: {},
        
    },
    getters: {
        // 获取 当前显示的 tab name
        GetCurrentTabIndex(state) {
            return state.currentTabIndex
        },
        // 获取 主页 tab 的 menuId
        GetHomeTabMenuId(state) {
            return state.homeTabMenuId
        },
        // 获取 当前打开的 tab 列表
        GetOpenedTabs(state) {
            return state.openedTabs
        },
        // 获取查询参数
        GetQuery(state) {
            let res = Object.create(null)
            state.openedTabs.map(i => {
                if (i.menuId === state.currentTabIndex) {
                    let end = i.components.length - 1
                    res = i.components[end].query || {}
                    return
                }
            })
            return res
        },
        // 获取当前激活的 tab
        GetCurrentTab(state) {
            return state.openedTabs.find(i => i.menuId === state.currentTabIndex)
        },

        //获取刷新
        GetRefresh(state) {
            return state.refresh
        }
    },
    mutations: {
        // 设置 主页 tab
        SetHomeTab(state, item) {
            state.homeTabMenuId = item.menuId
            state.openedTabs[0] = item
        },
        // 回显 tab
        reShowHash(state) {
            let url = location.href
            let indexOfSharp = url.indexOf('#')

            // if (indexOfSharp > 0) {
            //     let hash = url.substr(indexOfSharp + 1)
            //         // let tab = JSON.parse(Base64.decode(hash))
            //     console.log(hash)
            //     let tab = JSON.parse(hash)
            //     if (tab.menuId === state.homeTabMenuId) {
            //         state.openedTabs = state.openedTabs.filter(i => i.menuId !== state.homeTabMenuId)
            //     }
            //     state.openedTabs.push(tab)
            //     state.currentTabIndex = tab.menuId
            // }
        },
        // 设置 当前显示的 tab name
        SetCurrentTabIndex(state, data) {
            state.currentTabIndex = data
        },
        // 添加 tab 到 tab 列表
        OpenedTabsPush(state, item) {
            // 设置当前要显示的 tab name
            
            state.currentTabIndex = item.menuId;
            // 判断 tab 项是否已存在
            let tabExsit = state.openedTabs.find(i => i.menuId === item.menuId);
            // console.log(tabExsit)
            if (!tabExsit) {
                if (item.menuId == state.homeTabMenuId) {
                    state.currentTabIndex = state.openedTabs[tabsId].menuId
                } else {
                    state.openedTabs.push({
                        title: item.title, // 显示标题
                        menuId: item.menuId, // 用于标记当前打开 tab 的 name
                        refresh: true,
                        refreshNumber: 1,
                        components: [{
                            path: item.component
                        }], //  tab 对应的组件
                    })
                }
            } else if (item.fromHistory) { // 点击前进后退按钮
                state.openedTabs.map(i => {
                    if (i.menuId === item.menuId) {
                        i.components = item.components
                    }
                })
            }
        },
        // 当前 tab 内部的跳转
        OpenedSubTabsPush(state, item) {
            store.commit('SearchItem', item);
            let tabExsit = state.openedTabs.find(i => i.menuId === state.newItem.menuId);
            if (!tabExsit) {
                state.openedTabs.push({
                    title: state.newItem.title, // 显示标题
                    menuId: state.newItem.menuId, // 用于标记当前打开 tab 的 name
                    refresh: true,
                    refreshNumber: 1,
                    components: [item], //  tab 对应的组件
                })
            }
            state.currentTabIndex = state.newItem.menuId;
            // else {
            //     tabExsit.refreshNumber = 2;
            //     // tabExsit.components = [item];
            //     store.commit('OpenedTabsRemove', state.newItem.menuId);
            //     // state.currentTabIndex = state.newItem.menuId;

            //     store.commit('OpenedSubTabsReplace', item);
            // }
        },
        // 返回
        OpenedSubTabsBack(state, num = 1) {
            if (num < 1) num = 1
            state.openedTabs.map(i => {
                if (i.menuId === state.currentTabIndex) {
                    let newLength = i.components.length - ~~num
                    console.log(newLength)
                    if (newLength > 0) {
                        i.components = i.components.slice(0, newLength)
                    }
                }
            })
        },
        // 替换当前组件
        OpenedSubTabsReplace(state, item) {
            let index = state.openedTabs.length - 1
            state.openedTabs.map(i => {
                if (i.menuId === state.currentTabIndex) {
                    i.components.splice(index, 1, item)
                }
            })
        },
        // 从 tab 列表 移除 tab
        OpenedTabsRemove(state, menuId) {
            var tabsId = '';
            state.openedTabs = state.openedTabs.filter((item, index) => {
                if (item.menuId == menuId) {
                    tabsId = index
                }
                return item.menuId === state.homeTabMenuId || item.menuId !== menuId
            });

            // 查询当前标签是否被关闭，如果被关闭，则打开主页标签
            let tab = state.openedTabs.find(item => item.menuId === menuId);
            if (!tab) {
                if (tabsId > state.openedTabs.length) {
                    state.currentTabIndex = state.homeTabMenuId
                } else if (tabsId == state.openedTabs.length) {
                    state.currentTabIndex = state.openedTabs[tabsId - 1].menuId;
                    console.log(state.currentTabIndex)
                } else {
                    state.currentTabIndex = state.openedTabs[tabsId].menuId
                }
            }
        },
        // 从 tab 列表 移除 tab  根据名称
        OpenedTabsRemoveName(state, name) {
            var tabsId = '';
            state.openedTabs = state.openedTabs.filter((item, index) => {
                if (item.components[0].path !== name) {
                    tabsId = index
                }
                return item.components[0].path === state.homeTabMenuId || item.components[0].path !== name
            });
            // 查询当前标签是否被关闭，如果被关闭，则打开主页标签
            let tab = state.openedTabs.find(item => item.components[0].path === name);
            if (!tab) {
                if (tabsId >= state.openedTabs.length) {
                    state.currentTabIndex = state.homeTabMenuId
                } else if (tabsId == state.openedTabs.length) {
                    state.currentTabIndex = state.openedTabs[tabsId - 1].menuId
                } else {
                    state.currentTabIndex = state.openedTabs[tabsId].menuId
                }
            }
        },
        // 关闭 其他的 tab
        CloseOthersTabs(state) {
            state.openedTabs = state.openedTabs.filter(item => {
                return item.menuId === state.homeTabMenuId || item.menuId === state.currentTabIndex
            })
        },
        // 关闭所有 tab
        CloseAllTabs(state) {
            state.openedTabs.length = 1
            state.currentTabIndex = state.homeTabMenuId
        },
        //查询单个item
        SearchItem(state, item) {
            conf.menu.forEach(el => {
                if (el.sub.length > 0) {
                    el.sub.forEach(i => {
                        if (i.component == item.path) {
                            state.newItem = i;
                        }
                    })
                }
            });

        }
    },
    actions: {},
}